package com.txby.hisJob.component.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class LoginInterceptor implements HandlerInterceptor  {
	private static Logger log = LoggerFactory
			.getLogger(LoginInterceptor.class);
	
	 @Override
	    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
	            throws Exception {
	    	log.info("LoginInterceptor在请求处理之前进行调用（Controller方法调用之前）");
/*//			请求路径
			String path=request.getServletPath();
			path=path.substring(path.lastIndexOf("/")+1, path.length());
//			项目路径
			 String contextPath = request.getContextPath();
			String appUrl="verifyLogin|updatePassWord|findOneMac|findOneMacPart|getTopData|getRegionMacData|getRegionDetailMacData|getRegionDetailMacUseLoc|getGasMacData|getGasDetailData|getMacChekData|verifyData|" +
					"getMacDetailChekData|getElecCheckMacData|gasQuantitySave|uploadMacPic|downLoadMacPartImg|updateUserPassWord|signMcsSave|findTBV|signRFIDSave|saveAppLogInfo|getGasQRCodeInfo|getTopMacInfoData|getRfidIsBound|getMcsIsBound|findEquipmentTypeInfo|findMcsAllInfo";
				if(path.endsWith("login.jsp")||path.endsWith("image.jsp")||path.endsWith("404.jsp")||path.endsWith("error.jsp")){
					return true;
				}else{
					request.getRequestDispatcher("/jsp/error/error.jsp").forward(request, response);
					return false;
				}
			
			if(appUrl.contains(path)){
				return true;
			}else{
//				if(path.endsWith(".woff")||path.endsWith(".ttf")){
//					return true;
//				}
				if(path.endsWith(".htm")){
					if(path.contains("tlogin.htm")||path.contains("checkImageValue.htm")||path.contains("login.htm")
							||path.contains("logOut.htm")||path.contains("error.htm")||path.contains("signUser.htm")
							||path.contains("checkPassWord.htm")||path.contains("signUserSave.htm")||path.contains("isExist.htm")||path.contains("isExistRegUser.htm")||path.contains("updatePassWord.htm")||path.contains("reloading.htm")
							
							){
						return true;
					}else{
						Gzz01UserData user=(Gzz01UserData)request.getSession().getAttribute("sysuser");
						if(user==null){
							response.sendRedirect(contextPath+"/login/reloading.htm");
//							response.sendRedirect(contextPath+"/tlogin.htm");//跳转jsp过滤直接访问s
//							request.getRequestDispatcher(contextPath+"/login.jsp").forward(request, response);
							return false;
						}else {
							return true;
						}
					}
				}else{
					response.sendRedirect(contextPath+"/login/error.htm");
					return false;
				}

			}*/
			return true;
//	        return true;// 只有返回true才会继续向下执行，返回false取消当前请求
	    }

	    @Override
	    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
	            ModelAndView modelAndView) throws Exception {
	    	log.info("LoginInterceptor请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
	    }

	    @Override
	    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
	            throws Exception {
	    	log.info("LoginInterceptor在整个请求结束之后被调用，也就是在DispatcherServlet 渲染视图之后执行（主要是用于进行资源清理工作）");
	    }
}